import sys
import platform
import asyncio

import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options

from log import Log
from application import Application

if sys.version_info.major >= 3 and sys.version_info.minor >= 8:
    if platform.system() == "Windows":
        asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


tornado.options.define("port", default=8190, type=int, help="run server on the given port.")  # 定义服务器监听端口选项
tornado.options.define("kernel_ip", default="127.0.0.1", type=str, help="run kernel on the given ip.")
tornado.options.define("kernel_port", default=8555, type=int, help="run kernel on the given port.")

if __name__ == "__main__":
    try:
        logger = Log().logger
        tornado.options.parse_command_line()
        logger.info(f"server start by port {options.port} and kernel_port is {options.kernel_port}")
        app = Application(options.port, options.kernel_ip, options.kernel_port)
        httpServer = tornado.httpserver.HTTPServer(app)
        httpServer.listen(options.port)
        tornado.ioloop.IOLoop.current().start()
    except KeyboardInterrupt:
        pass
